package com.lz.kunkun.messageservice.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ImageDownloadController {
	/**
	 * @author zhangkun 返回图片二进制流(针对upload.private里面的)
	 * @param url
	 *            查询条件: 图片地址
	 * @return
	 */
	@GetMapping("img")
	public @ResponseBody void getImage(String url, HttpServletResponse response) {
		ServletOutputStream out = null;
		FileInputStream ips = null;
		try {
			File file = new File(url);
			if (file.exists()) {
				ips = new FileInputStream(new File(url));
				// 获取图片存放路径
				ips = new FileInputStream(new File(url));
				response.setContentType("image/jpg");
				out = response.getOutputStream();
				// 读取文件流
				int len = 0;
				byte[] buffer = new byte[1024 * 10];
				while ((len = ips.read(buffer)) != -1) {
					out.write(buffer, 0, len);
				}
				out.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (ips != null) {
					ips.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
